﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Ys.Base.Common
{
    /// <summary>
    /// LIst<T>拓展
    /// </summary>
   public static class ListExtension
    {
        

       
        /// <summary>
        /// 只塑性对象中的list数据 非 list不塑性处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="field">指定的字段名称 ,间隔</param>
        /// <returns></returns>
        public static ExpandoObject GetFieldListData<T>(this T t, string field)
        {
            var getdata =new ExpandoObject();
          
            foreach (var pro in t.GetType().GetProperties()) {
                var type = pro.PropertyType;
              
                    var tlist = type.GetInterfaces();
                    var tsss = typeof(IEnumerable<>);
                    var ltss = typeof(IList<>);
              
                if (type.GetInterfaces().Where(w=>w.Name==typeof(IList<>).Name).Count()>0)
                {
                    var list = new List<ExpandoObject>();
                    var listvalue = pro.GetValue(t, null) as IEnumerable<object>;
                    if (listvalue.Count() > 0)
                    {
                        var r = listvalue.First();
                        var Propertieslist = r.GetType().GetRuntimeProperties();
                        if (string.IsNullOrWhiteSpace(field))
                        {
                            foreach (var ritem in Propertieslist)
                            {
                                field = field.Length > 0 ? field+=","+ritem.Name :ritem.Name;
                            }
                        }
                      
                        var fieidlist = field.Split(',');
                        foreach (var item in listvalue)
                        {
                            var pair = new ExpandoObject();
                            foreach (var txt in fieidlist.Reverse()) {
                                if (Propertieslist.Where(w => w.Name.ToLower() == txt.ToLower()).Count() > 0)
                                {
                                    var property = r.GetType().GetProperty(txt, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                                    pair.TryAdd(property.Name, property.GetValue(item, null));
                                }
                            }
                            list.Add(pair);
                        }
                    }
                    var getlistss = list;
                    getdata.TryAdd(pro.Name, list);
                }
                else
                    {
                        getdata.TryAdd(pro.Name, pro.GetValue(t,null));
                    }
                }

           
            return getdata;
        }

        /// <summary>
        /// 数据塑性List返回指定的数据字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="field">指定的字段名称 ,间隔</param>
        /// <returns></returns>
        public static IEnumerable<ExpandoObject> GetFieldListData<T>(this IEnumerable<T> t, string field)
            {
                var pairs = new List<ExpandoObject>(t.Count());


                if (string.IsNullOrWhiteSpace(field))
                {
                    var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                    foreach (var item in t)
                    {
                        var pair = new ExpandoObject();
                        foreach (var property in propertyInfos)
                        {
                            pair.TryAdd(property.Name, property.GetValue(item, null));
                        }
                        pairs.Add(pair);
                    }
                    return pairs;
                }
                var fieldSpits = field.Split(',');

                foreach (var item in t)
                {
                    var pair = new ExpandoObject();
                    foreach (var txt in fieldSpits.Reverse())
                    {
                        var property = typeof(T).GetProperty(txt, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                        pair.TryAdd(property.Name, property.GetValue(item, null));
                    }
                    pairs.Add(pair);
                }
                return pairs;
            }
        
    }
}
